feat(ai/settings): surface, reject, and recover AI provider misconfiguration#1788
Merged
Conversation
…uration A half-saved `ai` settings row (provider=cloudflare, empty key) silently overrode env auto-detection and the only symptom was a bare "Bad Request" in chat. Close every gap in that chain: - GET /api/v1/ai/status — active adapter provenance (source: explicit/ env/settings/fallback, provider, model) plus settingsError explaining why saved settings were NOT applied. - Server-side save validation in SettingsService.setMany: visible+required empty fields and pattern mismatches reject the batch with field-level errors (400 SETTINGS_VALIDATION). Visibility expressions are evaluated by a restricted-grammar parser; resets and unparseable expressions stay lenient. gateway_model/cloudflare_model gain provider/model patterns. - Built-in `reset` settings action per namespace; the ai override also re-runs env adapter detection. Manifest ships a "Reset to environment defaults" button — no more hand-editing sys_setting. - Chat/agent/assistant stream errors now carry the adapter description and actionable hints (400 → model-id format, 401 → credential, …). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
本地 showcase 曾出现:Setup UI 半填的 AI 设置(provider=cloudflare、API key 为空)被存进
sys_setting,静默覆盖了 env 自动检测出的 adapter;唯一症状是聊天流里一句裸的Bad Request。排查只能靠 sqlite 手术。本 PR 把这条链路上的四个缺口全部补上(服务端部分;前端按钮/错误展示由 manifest 与现有 toast 机制驱动,objectui 零改动)。改动
1. 生效配置可见 —
GET /api/v1/ai/statusAIServicePlugin全程追踪 adapter 来源(boot 检测 / settings 重建 / reset),新接口返回:{ "adapter": "vercel", "description": "Vercel AI Gateway (model: …)", "source": "env | settings | explicit | fallback", "provider": "gateway", "model": "…", "settingsProvider": "…", "settingsError": null }settingsError非空 = 已保存的 settings 没有生效及其原因(此前只有一行服务端 warn)。2. 保存时服务端校验(spec 早已承诺,首次实现)
SettingsService.setMany现在按 manifest 校验:对"当前可见(visible表达式服务端求值)+ required + 被本次写入触及"的字段拒绝空值;pattern不匹配同样拒绝。整批原子拒绝,400 SETTINGS_VALIDATION携带字段级错误,UI toast 直接可读。受限文法求值器见visibility-eval.ts(data.x === 'y'、&&/||/!、括号、字面量);不可解析表达式与全空 patch(重置)保持宽松。gateway_model/cloudflare_model增加provider/model格式 pattern。3. 一键恢复 — 内置
resetaction + manifest 按钮每个 settings 命名空间获得内置
reset(清持久化行,回落 env/默认,留审计);ai命名空间的覆盖版还会立即重跑 env adapter 检测。AI manifest 增加 "Reset to environment defaults" 按钮,前端自动渲染。4. chat 流错误可诊断
chat/agents/assistant 三条流式路由的 provider 错误经
error-hints.ts增强:带 adapter 描述 + 按状态码映射的下一步提示(400→model id 格式、401/403→凭据、404→模型不存在、429→限流、网络错误)。Bad Request变成:测试
adapter-status.test.ts(8)、error-hints.test.ts(6)、visibility-eval.test.ts(6)+ settings reset/校验用例;service-ai 359、service-settings 113 全绿。gpt-4o无斜杠被 pattern 拒;存入合规但错误的 model 后 status 立即显示source: settings+ 该 model;chat 错误带完整诊断;点 Reset 按钮 toast 报告 "Cleared 2 saved value(s). Active adapter: …" 且 chat 即刻恢复。🤖 Generated with Claude Code